package com.hotel.order.service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import com.hotel.order.mapper.OrderMapper;
import com.hotel.order.pojo.Order;
import com.hotel.order.pojo.OrderResult;
import com.hotel.order.pojo.Room;
import com.hotel.order.pojo.User;
import com.hotel.order.utils.MyException;

@Transactional
@Service
public class OrderService {

	@Autowired
	private OrderMapper orderMapper;
	
	@Autowired
	private RestTemplate restTemplate;
	
	// 查询全部订单
	public List<OrderResult> selectAllOrder() {
		
		List<Order> list= orderMapper.selectAllOrder();
		List<OrderResult> relist=new ArrayList<OrderResult>(); 
		for (Order order : list) {
			//根据订单的userid获取userName 封装后进行返回
			String url="http://userservice/user/manage/query1?id="+order.getUserId();
			//校验订单信息和用户表是否符合  TODO
			String userName=restTemplate.getForObject(url, User.class).getUserName();
			OrderResult orderResult=new OrderResult();
			orderResult.setId(order.getId());
			orderResult.setArriveTime(order.getArriveTime());
			orderResult.setLeaveTime(order.getLeaveTime());
			orderResult.setPhone(order.getPhone());
			orderResult.setState(order.getState());
			orderResult.setRoomType(order.getRoomType());
			orderResult.setUserName(userName);
			relist.add(orderResult);
		}
		return relist;
	}
	
	//后台添加订单
	public void addOrder(Order order, String userName) throws MyException {
		//根据用户名查找用户 
		 String url="http://userservice/user/manage/findUserByUserName?userName="+userName;
		 User user = restTemplate.getForObject(url, User.class);
		 if(user==null){
			 throw new MyException("该用户尚未注册");
		 }
		//用户存在  获取补充order信息 
		order.setUserId(user.getId());
		order.setId(UUID.randomUUID().toString());
		order.setState(0);
		//查询room  判断是否还有房间
		 String uri1="http://roomservice/room/manage/selectRoomByType?roomType="+order.getRoomType();
		 Room room=restTemplate.getForObject(uri1, Room.class);
		 if(room==null){
			 throw new MyException("该房间没有空房");
		 }
		//根据roomType更新room的num
		 int num=-1;
		 String uri="http://roomservice/room/manage/updateRoomNumByType?roomType="+order.getRoomType()+"&num="+num;
		 restTemplate.getForObject(uri, String.class);
		//添加订单
		orderMapper.insertOrder(order);
	}

	//删除订单
	public void deleteOrderById(String id,String roomType) {
		int num=1;
		//根据房间类型为room加num
		 String url="http://roomservice/room/manage/updateRoomNumByType?roomType="+roomType+"&num="+num;
		 String result=restTemplate.getForObject(url, String.class);
		//删除订单
		orderMapper.deleteOrderById(id);
	}
	
	//前台 根据用户id查询订单-薛凯
	public List<Order> queryMyOrders(String userId) {
		List<Order> list =  orderMapper.selectOrdersByUserId(userId);
		return list;
	}

	public void deleteOrderByUserId(String userId) {
		orderMapper.deleteOrderByUserId(userId);
	}
	
	
	
	
	
	
	
	
	

}
